package com.njzxw.fs.commonutil.imp;

import com.njzxw.fs.commonutil.bean.FileBean;
import com.njzxw.fs.commonutil.repository.BaseRep;
import com.njzxw.fs.commonutil.util.FtpUtils;
import com.njzxw.fs.commonutil.util.StringHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class FtpOssImp extends OssUtilAbs{

    @Autowired
    private FtpUtils ftpUtils;

    @Autowired
    private BaseRep baseRep;

    @Override
    public boolean connect() {
        return ftpUtils.initFtpClient();
//        return true;
    }

    @Override
    public FileBean saveTemp(InputStream inputStream,String name,long size) {

        FileBean fileBean = exeName(name,size);
        if(fileBean == null){
            logger.warn("文件丢失");
            return null;
        }
        logger.debug("上传文件："+fileBean.toString());
        boolean truth = ftpUtils.uploadFile(common.getSystem("RESOURCE.TEMP.PATH"),fileBean.getFile_name(),inputStream);
        logger.debug("上传truth:"+truth);
        if(!truth){
            return null;
        }

        int exc = baseRep.insertTemp(fileBean);

        if(exc == 1){
            return fileBean;
        };

        return null;
    }

    @Override
    public boolean close() {
        return ftpUtils.disconnect();
    }

    @Override
    public String exeFile(String id, String path) {
        FileBean fileBean = baseRep.getTempFile(id);
        logger.debug("fileBean:"+fileBean);
        if(StringUtils.isBlank(fileBean.getFile_name())){
            return "";
        }

        //临时下载文件
        URL url = null;
        try {
            System.out.println(fileBean.getImg_host()+fileBean.getFile_name());
            url = new URL(fileBean.getImg_host()+fileBean.getFile_name());
            // 打开连接
            URLConnection con = url.openConnection();
            //设置请求超时为10s
            con.setConnectTimeout(10 * 1000);

            fileBean.setImg_host(common.getSystem("RESOURCE.IMGHOST")+path+"/");
            fileBean.setPath(path);

            if(!ftpUtils.initFtpClient()){
                return "";
            }

            if(ftpUtils.uploadFile(path,fileBean.getFile_name(),con.getInputStream())){
                if(baseRep.insertFile(fileBean) != 1){
                    return "";
                }
                return String.valueOf(fileBean.getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        } finally {
            ftpUtils.disconnect();
        }

        return "";
    }

    @Override
    public String exeFile(String id, String path, String oper_id, int oper_type, String oper_name) {
        return null;
    }

    @Override
    public List<String> exeFile(List<String> ids, String path) {

        List<String> returnList = new ArrayList<>();
        try{
            if(!ftpUtils.initFtpClient()){
                return null;
            }

            for(int i=0;i<ids.size();i++){
                String id = ids.get(i);
                FileBean fileBean = baseRep.getTempFile(id);
                logger.debug("fileBean:"+fileBean);
                if(StringUtils.isBlank(fileBean.getFile_name())){
                    return null;
                }

                URL url = new URL(fileBean.getImg_host()+fileBean.getFile_name());

                // 打开连接
                URLConnection con = url.openConnection();
                //设置请求超时为10s
                con.setConnectTimeout(10 * 1000);

                fileBean.setImg_host(common.getSystem("RESOURCE.IMGHOST")+path+"/");
                fileBean.setPath(path);

                if(!ftpUtils.initFtpClient()){
                    return null;
                }

                if(ftpUtils.uploadFile(path,fileBean.getFile_name(),con.getInputStream())){
                    if(baseRep.insertFile(fileBean) != 1){
                        return null;
                    }
                    returnList.add(String.valueOf(fileBean.getId()));
                }

            }

            return returnList;

        }catch (Exception e){
            logger.error("文件正式存储失败：",e);
            return null;
        }finally {
            ftpUtils.disconnect();
        }
    }

    @Override
    public List<String> exeFile(List<String> ids, String path,String oper_id,int oper_type,String oper_name) {

        List<String> returnList = new ArrayList<>();
        try{
            if(!ftpUtils.initFtpClient()){
                return null;
            }

            for(int i=0;i<ids.size();i++){
                String id = ids.get(i);
                FileBean fileBean = baseRep.getTempFile(id);
                logger.debug("fileBean:"+fileBean);
                if(StringUtils.isBlank(fileBean.getFile_name())){
                    return null;
                }

                URL url = new URL(fileBean.getImg_host()+fileBean.getFile_name());

                // 打开连接
                URLConnection con = url.openConnection();
                //设置请求超时为10s
                con.setConnectTimeout(10 * 1000);

                fileBean.setImg_host(common.getSystem("RESOURCE.IMGHOST")+path+"/");
                fileBean.setPath(path);

                if(!ftpUtils.initFtpClient()){
                    return null;
                }

                if(ftpUtils.uploadFile(path,fileBean.getFile_name(),con.getInputStream())){
                    if(baseRep.insertFile(fileBean) != 1){
                        return null;
                    }
                    returnList.add(String.valueOf(fileBean.getId()));
                }

            }

            return returnList;

        }catch (Exception e){
            logger.error("文件正式存储失败：",e);
            return null;
        }finally {
            ftpUtils.disconnect();
        }
    }

}
